package model.dba;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import model.Instructor;
import model.Tipo_Empleado;

public class InstructorDAO extends JDBC {
	private static InstructorDAO personalAdministrativoDAO;

	private InstructorDAO() {
		super();
	}

	public static InstructorDAO getInstance() {
		if (personalAdministrativoDAO == null) {
			personalAdministrativoDAO = new InstructorDAO();
		}
		return personalAdministrativoDAO;
	}

	public Integer add(Instructor instructor) {

		try {
			String queryString = "INSERT INTO instructores(dni, nombre, apellido, direccion, tel, cel, fechaNac,idusuario)" + " VALUES(?, ?, ?, ?, ?, ?,?,?) ";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setString(1, instructor.getDNI());
			preparedStatement.setString(2, instructor.getNombre());
			preparedStatement.setString(3, instructor.getApellido());
			preparedStatement.setString(4, instructor.getEmail());
			preparedStatement.setString(5, instructor.getTelefono());
			preparedStatement.setString(6, instructor.getCelular());
			preparedStatement.setTimestamp(7, new Timestamp(instructor.getNacimiento().getTimeInMillis()));
			preparedStatement.setLong(8, instructor.getIdUsuario());// TODO revisar si funciona

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	public boolean update(Instructor instructor) {
		boolean state = false;

		try {
			String queryString = "UPDATE instructores SET  nombre=?, apellido=?,  direccion=?, tel=?, cel=?, fechaNac=? WHERE dni=?";// , idusuario=?

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setString(1, instructor.getNombre());
			preparedStatement.setString(2, instructor.getApellido());
			preparedStatement.setString(3, instructor.getEmail());
			preparedStatement.setString(4, instructor.getTelefono());
			preparedStatement.setString(5, instructor.getCelular());
			preparedStatement.setTimestamp(6, new Timestamp(instructor.getNacimiento().getTimeInMillis()));
			// preparedStatement.setLong(8, instructor.getIdUsuario());// usuario
			preparedStatement.setString(7, instructor.getDNI());// Cambie por 7
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Instructor instuctor) {
		boolean state = false;

		try {
			String queryString = "DELETE FROM instuctores WHERE dni=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setString(1, instuctor.getDNI());
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public Instructor get(String DNI) {

		Instructor instructor = new Instructor();
		try {
			String queryString = "SELECT * FROM instructores where dni = '" + DNI + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				instructor = populate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return instructor;
	}

	public Instructor find(int idusuario) {

		Instructor instructor = new Instructor();
		try {
			String queryString = "SELECT * FROM instructores where idusuario = '" + idusuario + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				instructor = populate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return instructor;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT dni FROM instructores order by dni DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("dni");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<Instructor> getAll() {
		List<Instructor> instructoresList = new ArrayList<Instructor>();

		try {
			String queryString = "SELECT * FROM instructores;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				instructoresList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return instructoresList;
	}

	public List<Instructor> get(Tipo_Empleado tipo) {
		List<Instructor> instructoresList = new ArrayList<Instructor>();

		try {
			String queryString = "SELECT * FROM instructores where tipo = '" + tipo.toString() + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				instructoresList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return instructoresList;
	}

	public List<Instructor> find(String nombre, String apellido) {
		List<Instructor> instructoresList = new ArrayList<Instructor>();

		try {
			String queryString = "SELECT * FROM instructores where nombre = '" + nombre + "' and apellido= '" + apellido + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				instructoresList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return instructoresList;
	}

	private Instructor populate() throws SQLException {
		// OBSERVAR SI TENDRA UN USUAIRO
		Instructor instructor = new Instructor();
		instructor.setApellido(resultSet.getString("apellido"));
		instructor.setCelular(resultSet.getString("cel"));
		instructor.setDNI(resultSet.getString("dni"));
		instructor.setEmail(resultSet.getString("direccion"));
		GregorianCalendar nacimiento = new GregorianCalendar();
		nacimiento.setTime(resultSet.getTimestamp("fechaNac"));
		instructor.setNacimiento(nacimiento);
		instructor.setNombre(resultSet.getString("nombre"));
		instructor.setTelefono(resultSet.getString("tel"));
		instructor.setEliminado(resultSet.getString("eliminado"));
		instructor.setIdUsuario((int) resultSet.getLong("idusuario"));

		// personalAdministrativo.setTipo(Tipo_Empleado.valueOf(resultSet.getString("tipo")));

		return instructor;

	}

	public boolean updateBaja(Instructor instructor) {
		boolean state = false;

		try {
			String queryString = "UPDATE instructores SET  eliminado=? WHERE dni=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setString(1, instructor.getEliminado());
			preparedStatement.setString(2, instructor.getDNI());
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

}